
const Namespaces = require('./Namespaces');

class DomSelectedChecker {
    protected targetElement: any;
    constructor(targetElement?: any) {
        this.targetElement = targetElement;
    }

    check(event, t) {
        var target = this.targetElement ? this.targetElement : (t ? t : Dom.getTarget(event));
        return target.checked || target.selected;
    };
}

class Dom {
    public static SELECTED_CHECKER = new DomSelectedChecker();

    protected static htmlEncodePlaceHolder: HTMLElement;

    protected static calculatedSystemScrollbarSize: {
        w: number,
        h: number,
    }

    static workOn(xpath, node, worker) {
        var nodes = this.getList(xpath, node);

        for (var i = 0; i < nodes.length; i++) {
            worker(nodes[i]);
        }
        return nodes.length;
    };

    static getText(node) {
        return node.textContent;
    };

    static getSingle(xpath, node) {
        if (document.all) {
            node.ownerDocument.setProperty("SelectionLanguage", "XPath");
            return node.selectSingleNode(xpath);
        }
        var doc = node.ownerDocument ? node.ownerDocument : node;
        var xpathResult = doc.evaluate(xpath, node, Namespaces.resolve, XPathResult.ANY_TYPE, null);
        return xpathResult.iterateNext();
    };

    static getList(xpath, node) {
        //alert(node.selectNodes);
        if (document.all) {
            node.ownerDocument.setProperty("SelectionLanguage", "XPath");
            return node.selectNodes(xpath);
        }

        var doc = node.ownerDocument ? node.ownerDocument : node;
        var xpathResult = doc.evaluate(xpath, node, Namespaces.resolve, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
        var nodes = [];
        var next = xpathResult.iterateNext();
        while (next) {
            nodes.push(next);
            next = xpathResult.iterateNext();
        }

        return nodes;
    }

    static setEnabled(enabled) {
        for (var i = 1; i < arguments.length; i++) {
            arguments[i].disabled = !enabled;
            if (enabled) {
                arguments[i].removeAttribute("disabled");
            } else {
                arguments[i].setAttribute("disabled", "true");
            }
        }
    };

    static getChildValue(xpath, node) {
        try {
            return this.getSingle(xpath, node).nodeValue;
        } catch (e) {
            return null;
        }
    };

    static isElementExistedInDocument(element) {
        while (element) {
            if (element == document) {
                return true;
            }
            element = element.parentNode;
        }
        return false;
    }

    static registerEvent(target, event, handler, capture) {
        if (!target) {
            //console.log("Can not register event to NULL target", event, handler);
            return;
        }
        if (event == "wheel") {
            window.addWheelListener(target, handler, capture);
            return;
        }

        var useCapture = false;
        if (capture) {
            useCapture = true;
        }
        if (target.addEventListener) {
            target.addEventListener(event, handler, useCapture);
        } else if (target.attachEvent) {
            target.attachEvent("on" + event, handler);
        }
    };

    static unregisterEvent(target, event, handler, capture) {
        var useCapture = false;
        if (capture) {
            useCapture = true;
        }
        if (target.removeEventListener) {
            target.removeEventListener(event, handler, useCapture);
        } else if (target.dettachEvent) {
            target.dettachEvent("on" + event, handler);
        }
    };

    static empty(node) {
        if (!node) return;
        while (node.firstChild) {
            node.removeChild(node.firstChild);
        }
    };

    static disableEvent(node, event) {
        this.registerEvent(node, event, function (ev) {
            this.cancelEvent(ev);
        }, true);
    };

    static getEvent(e) {
        return window.event ? window.event : e;
    };

    static getTarget(e) {
        if (!e) return {};
        var event = this.getEvent(e);
        return event.srcElement ? event.srcElement : event.originalTarget;
    };

    static getWheelDelta(e) {
        var event = this.getEvent(e);
        var delta = 0;
        if (event.wheelDelta) { /* IE/Opera. */
            delta = event.wheelDelta / 120;
            /** In Opera 9, delta differs in sign as compared to IE.
             */
        } else if (event.detail) { /** Mozilla case. */
            /** In Mozilla, sign of delta is different than in IE.
             * Also, delta is multiple of 3.
             */
            delta = -event.detail / 3;
        }

        return delta;
    };

    static emitEvent(name, sourceElement, options) {
        var evt = null;
        if (document.createEvent) {
            evt = document.createEvent("HTMLEvents");
            if (evt.initEvent) {
                evt.initEvent(name, true, false);
            }
        } else if (document.createEventObject) {
            evt = document.createEventObject();
            evt.eventType = name;
        }
        if (!evt) {
            //console.log("Can not find method create event to emit event" , name);
            return;
        }
        evt.eventName = name;

        for (const n in options) evt[n] = options[n];

        //console.log("emit event on " , sourceElement.tagName, name);
        if (sourceElement.dispatchEvent) {
            sourceElement.dispatchEvent(evt);
        } else if (sourceElement.fireEvent) {
            sourceElement.fireEvent('on' + evt.eventType, evt);
        } else if (sourceElement[name]) {
            sourceElement[name]();
        } else if (sourceElement['on' + name]) {
            sourceElement['on' + name]();
        }

    };

    static getEventOffset(e, to) {
        var x = 0;
        var y = 0;

        if (to) {
            x = this.getOffsetLeft(to);
            y = this.getOffsetTop(to);
        }

        var event = this.getEvent(e);
        if (typeof (event.pageX) != "undefined" || typeof (event.pageY) != "undefined") {
            return {
                x: event.pageX - x,
                y: event.pageY - y
            }
        } else {
            return {
                x: event.clientX + this.getScrollLeft() - x,
                y: event.clientY + this.getScrollTop() - y
            }
        }
    };

    static getTouchOffset(t, to) {
        var x = 0;
        var y = 0;

        if (to) {
            x = this.getOffsetLeft(to);
            y = this.getOffsetTop(to);
        }

        return {
            x: t.pageX - x,
            y: t.pageY - y
        }
    };

    static getEventScreenX(event) {
        if (event.touches && event.touches.length == 1) {
            return event.touches[0].screenX;
        } else {
            return event.screenX;
        }
    };

    static getEventScreenY(event) {
        if (event.touches && event.touches.length == 1) {
            return event.touches[0].screenY;
        } else {
            return event.screenY;
        }
    };

    static isMultiTouchEvent(event) {
        return event.touches && event.touches.length > 1;
    };

    static cancelEvent(e) {
        var event = this.getEvent(e);
        if (event.preventDefault) {
            event.preventDefault();
        } else if (event.stopPropagation) {
            event.stopPropagation();
        } else event.returnValue = false;
    };

    static cancelEventBubbling(e) {
        if (event.stopPropagation) {
            event.stopPropagation();
        } else {
            e.cancelBubble = true;
        }
    }

    static addClass(node, className) {
        if (!node) return;
        if ((" " + node.className + " ").indexOf(" " + className + " ") >= 0) return;
        node.className += " " + className;
    };

    static removeClass(node, className) {
        if (!node) return;
        if (node.className == className) {
            node.className = "";
            return;
        }
        var re = new RegExp("(^" + className + " )|( " + className + " )|( " + className + "$)", "g");
        var reBlank = /(^[ ]+)|([ ]+$)/g;
        node.className = node.className ? node.className.replace(re, " ").replace(reBlank, "") : "";
    };

    static toggleClass(node, className, add) {
        if (add) {
            this.addClass(node, className);
        } else {
            this.removeClass(node, className);
        }
    }


    static getOffsetLeft(control) {
        var offset = control.offsetLeft;
        var parent = control.offsetParent;
        if (parent) if (parent != control) return offset + this.getOffsetLeft(parent);
        return offset;
    };

    static getOffsetTop(control) {
        var offset = control.offsetTop;
        var parent = control.offsetParent;
        if (parent) if (parent != control) {
            var d = parent.scrollTop || 0;
            return offset + this.getOffsetTop(parent) - d;
        }
        return offset;
    };

    static getOffsetHeight(control) {
        return control ? control.offsetHeight : 0;
    };

    static getOffsetWidth(control) {
        return control ? control.offsetWidth : 0;
    };

    static getWindowHeight() {
        if (typeof (window.innerWidth) == 'number') {
            return window.innerHeight;
        } else if (document.documentElement &&
            (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            return document.documentElement.clientHeight;
        } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
            return document.body.clientHeight;
        }
        return 0;
    };

    static getWindowWidth() {
        if (typeof (window.innerWidth) == 'number') {
            return window.innerWidth;
        } else if (document.documentElement &&
            (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            return document.documentElement.clientWidth;
        } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
            return document.body.clientWidth;
        }
        return 0;
    };

    static getScrollTop() {
        if (typeof (window.pageYOffset) == 'number') {
            //Netscape compliant
            return window.pageYOffset;
        } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
            //DOM compliant
            return document.body.scrollTop;
        } else if (document.documentElement &&
            (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
            //IE6 standards compliant mode
            return document.documentElement.scrollTop;
        }
        return 0;
    };

    static getScrollLeft() {
        if (typeof (window.pageXOffset) == 'number') {
            //Netscape compliant
            return window.pageXOffset;
        } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
            //DOM compliant
            return document.body.scrollLeft;
        } else if (document.documentElement &&
            (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
            //IE6 standards compliant mode
            return document.documentElement.scrollLeft;
        }
        return 0;
    };

    static reformHTML(node) {
    };

    static appendAfter(fragment, node) {
        if (!node.parentNode) {
            return;
        }
        if (node.nextSibling) {
            node.parentNode.insertBefore(fragment, node.nextSibling);
        } else {
            node.parentNode.appendChild(fragment);
        }
        //Dom.reformHTML(node.parentNode);
    };

    static insertBefore(fragment, node) {
        if (!node.parentNode) {
            return;
        }
        node.parentNode.insertBefore(fragment, node);
        this.reformHTML(node.parentNode);
    };

    static appendParent(fragment, node) {
        if (!node.parentNode) {
            return;
        }
        node.parentNode.appendChild(fragment);
        this.reformHTML(node.parentNode);
    };

    static prependParent(fragment, node) {
        if (!node.parentNode) {
            return;
        }
        if (node.parentNode.childNodes.length > 0) {
            node.parentNode.insertBefore(fragment, node.parentNode.childNodes[0]);
        } else {
            node.parentNode.appendChild(fragment);
        }
        this.reformHTML(node.parentNode);
    };

    static append(fragment, node) {
        node.appendChild(fragment);
        this.reformHTML(node);
    };

    static prepend(fragment, node) {
        if (node.childNodes.length > 0) {
            node.insertBefore(fragment, node.childNodes[0]);
        } else node.appendChild(fragment);
        this.reformHTML(node);
    };

    static replace(fragment, node) {
        if (!node.parentNode) {
            return;
        }
        node.parentNode.replaceChild(fragment, node);
        this.reformHTML(node.parentNode);
    };

    static xmlToFragment(xml) {
        var doc = null;
        var wrappedXml = "<root>" + xml + "</root>";
        if (document.implementation.createDocument) {
            var parser = new DOMParser();
            doc = parser.parseFromString(wrappedXml, "text/xml");
        } else {
            doc = new ActiveXObject("Microsoft.XMLDOM");
            doc.loadXML(wrappedXml);
        }
        var fragment = doc.createDocumentFragment();
        var root = doc.documentElement;
        for (var i = 0; i < root.childNodes.length; i++) {
            fragment.appendChild(root.childNodes[i].cloneNode(true));
        }
        return fragment;

    };

    static importNode(doc, node, importChildren) {
        if (doc.importNode) return doc.importNode(node, importChildren);
        var i = 0;
        switch (node.nodeType) {
            case 11: // DOCUMENT FRAGMENT
                var newNode = doc.createDocumentFragment();
                if (importChildren) {
                    for (i = 0; i < node.childNodes.length; i++) {
                        var clonedChild = this.importNode(doc, node.childNodes[i], true);
                        if (clonedChild) newNode.appendChild(clonedChild);
                    }
                }
                return newNode;
            case 1: // ELEMENT
                var newNode = doc.createElement(node.nodeName);
                for (i = 0; i < node.attributes.length; i++) {
                    newNode.setAttribute(node.attributes[i].name, node.attributes[i].value);
                }
                if (importChildren) {
                    for (i = 0; i < node.childNodes.length; i++) {
                        var clonedChild = this.importNode(doc, node.childNodes[i], true);
                        if (clonedChild) newNode.appendChild(clonedChild);
                    }
                }
                return newNode;
            case 3: // TEXT
                return doc.createTextNode(node.nodeValue);
        }
        return null;
    };

    static get(id, doc) {
        var targetDocument = doc ? doc : document;
        return targetDocument.getElementById(id);
    };

    static getTags(tag, doc) {
        var targetDocument = doc ? doc : document;
        return targetDocument.getElementsByTagName(tag);
    };

    static getTag(tag, doc) {
        var targetDocument = doc ? doc : document;
        return targetDocument.getElementsByTagName(tag)[0];
    };

    static isChildOf(parent, child) {
        if (!parent || !child) {
            return false;
        }
        if (parent == child) {
            return true;
        }
        return this.isChildOf(parent, child.parentNode);
    };

    static findUpward(node, evaluator: Evaluator, limit?) {
        if (node == null || (limit && limit(node))) {
            return null;
        }
        if (evaluator.eval(node)) {
            return node;
        }
        return this.findUpward(node.parentNode, evaluator);
    };

    static doUpward(node, evaluator: Evaluator, worker) {
        if (node == null) {
            return;
        }
        if (evaluator.eval(node)) {
            worker.work(node);
        }
        return this.doUpward(node.parentNode, evaluator, worker);
    };

    static findChild(node, evaluator: Evaluator) {
        if (!node || !node.childNodes) return null;

        for (var i = 0; i < node.childNodes.length; i++) {
            var child = node.childNodes[i];
            if (evaluator.eval(child)) return child;
        }

        return null;
    };

    static doOnChild(node, evaluator: Evaluator, worker) {
        if (!node || !node.childNodes) return null;

        for (var i = 0; i < node.childNodes.length; i++) {
            var child = node.childNodes[i];
            if (evaluator.eval(child)) worker(child);
        }

    };

    static doOnAllChildren(node, worker) {
        this.doOnChild(node, DomAcceptAllEvaluator, worker);
    };

    static doOnChildRecursively(node, evaluator: Evaluator, worker) {
        if (!node || !node.childNodes) return null;

        for (var i = 0; i < node.childNodes.length; i++) {
            var child = node.childNodes[i];
            if (evaluator.eval(child)) worker(child);
            this.doOnChildRecursively(child, evaluator, worker);
        }

    };

    static findChildTag(node, tag) {
        return this.findChild(node, new DomTagNameEvaluator(tag));
    };

    static findChildWithClass(node, className) {
        return this.findChild(node, {
            eval: function (node) {
                return (" " + node.className + " ").indexOf(" " + className + " ") >= 0;
            }
        });
    };

    static findDescendantWithClass(node, className) {
        if (!node || !node.childNodes) return null;

        for (var i = 0; i < node.childNodes.length; i++) {
            var child = node.childNodes[i];
            if ((" " + child.className + " ").indexOf(" " + className + " ") >= 0) return child;
            var descendant = this.findDescendantWithClass(child, className);
            if (descendant) return descendant;
        }

        return null;
    };


    static findParentWithClass(node: Node, className: string) {
        return this.findUpward(node, {
            className: className,
            eval: function (node) {
                return (" " + node.className + " ").indexOf(" " + this.className + " ") >= 0;
            }
        }, null);
    };

    static findParentByTagName(node, tagName) {
        return this.findUpward(node, {
            tagName: tagName.toUpperCase(),
            eval: function (node) {
                return node.tagName && node.tagName.toUpperCase && (node.tagName.toUpperCase() == this.tagName);
            }
        }, null);
    }

    static findParentWithProperty(node, property) {
        if (node == null) {
            return null;
        }
        if (typeof (node[property]) != "undefined") {
            return node;
        }
        return this.findParentWithProperty(node.parentNode, property);
    };

    static findParentWithAttribute(node, attName, attValue) {
        if (node == null) {
            return null;
        }
        //alert(node);
        if (node.getAttribute) {
            var value = node.getAttribute(attName);
            if (value) {
                if (!attValue) return node;
                if (attValue == value) return node;
            }
        }
        return this.findParentWithAttribute(node.parentNode, attName, attValue);
    };

    static findNonEditableParent(node) {
        if (node == null) {
            return null;
        }
        return this.findNonEditableParent(node.parentNode);
    };

    static isTag(node, tagName) {
        return (node.tagName && node.tagName.toUpperCase && node.tagName.toUpperCase() == tagName.toUpperCase());
    };

    static hasClass(node, className) {
        return (" " + node.className + " ").indexOf(className) >= 0;
    };

    static findFirstChild(node, tagName) {
        for (var i = 0; i < node.childNodes.length; i++) {
            var child = node.childNodes[i];
            if (this.isTag(child, tagName)) {
                return child;
            }
        }
        return null;
    }

    static findFirstChildWithClass(node, className) {
        for (var i = 0; i < node.childNodes.length; i++) {
            var child = node.childNodes[i];
            if ((" " + child.className + " ").indexOf(" " + className + " ") >= 0) {
                return child;
            }
        }
        return null;
    };

    static findLastChild(node, tagName) {
        for (var i = node.childNodes.length - 1; i >= 0; i--) {
            var child = node.childNodes[i];
            if (this.isTag(child, tagName)) {
                return child;
            }
        }
        return null;
    }

    static getDocumentBody() {
        return document.getElementsByTagName("body")[0];
    };

    static attrEncode(s, preserveCR) {
        preserveCR = preserveCR ? '&#13;' : '\n';
        return ('' + s) /* Forces the conversion to string. */
            .replace(/&/g, '&amp;') /* This MUST be the 1st replacement. */
            .replace(/'/g, '&apos;') /* The 4 other predefined entities, required. */
            .replace(/"/g, '&quot;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            /*
            You may add other replacements here for HTML only
            (but it's not necessary).
            Or for XML, only if the named entities are defined in its DTD.
            */
            .replace(/\r\n/g, preserveCR) /* Must be before the next replacement. */
            .replace(/[\r\n]/g, preserveCR);
        ;
    };

    static htmlEncode(s, skipNewLineProcessing) {
        if (!this.htmlEncodePlaceHolder) {
            this.htmlEncodePlaceHolder = document.createElement("div");
        }
        this.htmlEncodePlaceHolder.innerHTML = "";
        this.htmlEncodePlaceHolder.appendChild(document.createTextNode(s));
        var html = this.htmlEncodePlaceHolder.innerHTML;
        if (!skipNewLineProcessing) {
            html = html.replace(/[\n]/g, "<br/>");
        }

        return html;
    };

    static htmlStrip(s) {
        if (!this.htmlEncodePlaceHolder) {
            this.htmlEncodePlaceHolder = document.createElement("div");
        }
        this.htmlEncodePlaceHolder.innerHTML = s;
        var t = this.getInnerText(this.htmlEncodePlaceHolder);
        this.htmlEncodePlaceHolder.innerHTML = "";
        return t;
    };

    static setInnerText(node, text, unit) {
        node.innerHTML = "";
        if (unit && text.length > 0) {
            node.appendChild(node.ownerDocument.createTextNode(text + " " + unit));
        } else {
            node.appendChild(node.ownerDocument.createTextNode(text));
        }

    };

    static setInnerLineText(node, line) {
        if (line.length > 0) {
            for (var i = 0; i < line.length; i++) {
                node.innerHTML += line[i];
                if (i < line.length - 1) {
                    node.innerHTML += "<br/>";
                }
            }
        }
    };

    static getInnerText(node) {
        if (document.all) return node.innerText;
        if (node.textContent) return node.textContent;
        if (node.firstChild && node.firstChild.value) return node.firstChild.value;

        return "";
    };

    static installBehavior(target, eventName, checker, handler) {
        this.registerEvent(target, eventName, function (e) {
            if (checker.check(e)) {
                handler.positive();
            } else {
                handler.nagative();
            }
        }, null);
        this.registerEvent(window, "load", function (e) {
            if (checker.check(null, target)) {
                handler.positive();
            } else {
                handler.nagative();
            }
        }, null);
    };

    static newDOMElement(spec, doc, holder) {
        var ownerDocument = doc ? doc : document;
        var e = spec._uri ? ownerDocument.createElementNS(spec._uri, spec._name) : ownerDocument.createElement(spec._name);

        for (let name in spec) {
            if (name.match(/^_/)) continue;

            if (name.match(/^([^:]+):(.*)$/)) {
                var prefix = RegExp.$1;
                var localName = RegExp.$2;
                var uri = Namespaces[prefix];
                e.setAttributeNS(uri, name, spec[name]);
            } else {
                e.setAttribute(name, spec[name]);
                if (name == "class") {
                    this.addClass(e, spec[name]);
                }
            }
        }

        if (spec._text) {
            e.appendChild(e.ownerDocument.createTextNode(spec._text));
        }
        if (spec._cdata) {
            e.appendChild(e.ownerDocument.createCDATASection(spec._cdata));
        }
        if (spec._html) {
            e.innerHTML = spec._html;
        }
        if (spec._children && spec._children.length > 0) {
            e.appendChild(this.newDOMFragment(spec._children, e.ownerDocument, holder || null));
        }

        if (holder && spec._id) {
            holder[spec._id] = e;
        }

        return e;
    };

    static newDOMFragment(specs, doc, holder) {
        var ownerDocument = doc ? doc : document;
        var f = ownerDocument.createDocumentFragment();

        for (var i in specs) {
            f.appendChild(this.newDOMElement(specs[i], ownerDocument, holder || null));
        }
        return f;
    };

    static disableControls(element, disabled) {
        var nodeName = element.nodeName.toUpperCase();
        if (nodeName == "INPUT" || nodeName == "TEXTAREA" || nodeName == "SELECT") {
            element.disabled = disabled;

            return element;
        } else if (element.childNodes) {
            var firstControl = null;
            for (var i = 0; i < element.childNodes.length; i ++) {
                var control = this.disableControls(element.childNodes[i], disabled);
                if (!firstControl) firstControl = control;
            }
            return firstControl;
        }
    };
    static calculateSystemScrollbarSize() {
        if (this.calculatedSystemScrollbarSize) {
            return this.calculatedSystemScrollbarSize;
        }

        var wrapper = document.createElement("div");
        wrapper.style.overflow = "scroll";
        wrapper.style.visibility = "hidden";
        wrapper.style.position = "absolute";

        var inner = document.createElement("div");
        inner.style.width = "10px";
        inner.style.height = "10px";
        wrapper.appendChild(inner);

        document.body.appendChild(wrapper);

        this.calculatedSystemScrollbarSize = {
            w: this.getOffsetWidth(wrapper) - this.getOffsetWidth(inner),
            h: this.getOffsetHeight(wrapper) - this.getOffsetHeight(inner)
        };

        document.body.removeChild(wrapper);

        return this.calculatedSystemScrollbarSize;
    };

    static getBoundingClientRect(target) {
        var box = target.getBoundingClientRect();
        var newBox = {
            top: box.top,
            bottom: box.bottom,
            left: box.left,
            right: box.right,
            width: box.right - box.left,
            height: box.bottom - box.top,
        };

        return newBox;
    };

    static getIframeDocument(target) {
        var doc = null;
        if (target.contentDocument) { // DOM
            doc = target.contentDocument;
        } else if (target.contentWindow) { // IE win
            doc = target.contentWindow.document;
        }
        return doc.body.innerHTML
    }
}


class DomTagNameEvaluator {
    protected tagName: string;
    constructor(tagName) {
        this.tagName = tagName.toUpperCase();
    }

    eval(node) {
        return node && node.tagName && node.tagName.toUpperCase && (node.tagName.toUpperCase() == this.tagName);
    };
}

interface Evaluator
{
    [propName: string]: any,
    eval(target): boolean;
}

var DomAcceptAllEvaluator: Evaluator = {
    eval: function (target) { return true; }
};

class DomValueIsChecker {
    protected value;
    constructor(value) {
        this.value = value;
    }

    check(event, t) {
        var target = t ? t : Dom.getTarget(event);

        if (target.value && target.value == this.value) {
            return true;
        }

        if (target.selectedIndex && target.options && target.options[target.selectedIndex]) {
            if (target.options[target.selectedIndex].value == this.value) {
                return true;
            }
        }

        return false;
    };
}

class DomValueInChecker {
    protected values;
    constructor(values) {
        this.values = values;
    }

    check = function (event, t) {
        var target = t ? t : this.getTarget(event);

        var value = null;
        if (target.value) {
            value = target.value;
        }

        if (target.selectedIndex && target.options && target.options[target.selectedIndex]) {
            value = target.options[target.selectedIndex].value;
        }

        if (value && this.values.indexOf("|" + value + "|") >= 0) {
            return true;
        }
        return false;
    };
}

class DomEnableToggleHandler {
    protected control;
    constructor(control) {
        this.control = control;
    }

    positive() {
        var firstControl = Dom.disableControls(this.control, false);
        if (firstControl && firstControl.focus) {
            firstControl.focus();
            firstControl.select();
        }
    };
    nagative() {
        Dom.disableControls(this.control, true);
    };
}

class DomVisibilityToggleHandler {
    protected control;
 constructor(control) {
    this.control = control;
}
positive() {
    this.control.style.display = "";
};
nagative() {
    this.control.style.display = "none";
};
}

export default Dom;
